<?php
	
	require_once($C->INCPATH.'conf_embed.php');
	require_once($C->INCPATH.'helpers/func_images.php');

	class newpost_ex extends newpost
	{
		public $post_type = 0;/* vincent 2011.2.17日增加，为医考扩展*/
		public $item_name ;/* vincent 2011.2.23日增加，为推荐名称*/
		public $telephone ;/* vincent 2011.2.23日增加，为推荐店铺电话*/
		public $address;/* vincent 2011.2.23日增加，为推荐店铺地址*/ 
	    public $city_id;/* songming 2011.3.20日增加，城市电话*/
	    public $contact;/* songming 2011.3.20日增加，联系人*/
		public $item_date;
		public $item_link;
		public function __construct()
		{
		    parent::__construct();  
		}  
        
		/*vincent创建 2010.1.17*/
		public function set_postType( $post_type ){
			$this->post_type = $post_type;
		}
		/*end vincent创建 2010.1.17*/
		
		/*vincent创建 2010.2.23*/
		public function set_item_name( $item_name ){
			$this->item_name = $item_name;
		}
		
		public function set_item_date( $item_date ){
			$this->item_date = $item_date;
		}
		public function set_item_link( $item_link){
			$this->item_link = $item_link;
		}
		
		public function set_telephone( $telephone ){
			$this->telephone = $telephone;
		}

		public function set_address( $address ){
			$this->address = $address;
		}
		
		public function set_contact( $contact ){
			$this->contact = $contact;
		}
		
		public function set_city_id( $city_id ){
			$this->city_id = $city_id;
		}
		/*end vincent创建 2010.2.23*/
		

		public function save()
		{
			if( empty($this->message) ) {
				return FALSE;
			}
			global $C;
			$db2		= & $this->network->db2;
			$is_private	= $this->to_user ? TRUE : FALSE;
			$db_api_id		= intval($this->api_id);
			$db_user_id		= intval($this->user->id);
			$db_group_id	= $this->group ? intval($this->group->id) : 0;
			$db_to_user		= $this->to_user ? intval($this->to_user->id) : 0;
			$db_message		= $db2->escape($this->message);
			$db_mentioned	= count($this->mentioned);
			$db_attached	= count($this->attached);
			$db_posttags	= intval($this->posttags);
			$db_date		= time();
			$db_ip_addr		= ip2long($_SERVER['REMOTE_ADDR']);
			
			$city_id		= $this->city_id;
			$item_name		= $this->item_name;
			$address		= $this->address;
			$item_date      = $this->item_date;
			$contact		= $this->contact;
			$telephone		= $this->telephone;
			$item_link      = $this->item_link;
			if( ! $this->id )
			{
				if( $is_private ) {
					$db2->query('INSERT INTO posts_pr SET api_id="'.$db_api_id.'", user_id="'.$db_user_id.'", to_user="'.$db_to_user.'", message="'.$db_message.'", mentioned="'.$db_mentioned.'", posttags="'.$db_posttags.'", attached="'.$db_attached.'", date="'.$db_date.'", date_lastcomment="'.$db_date.'", ip_addr="'.$db_ip_addr.'", city_id="'.$city_id.'", item_link ="'.item_link.'",item_name="'.$item_name.'", address="'.$address.'", telephone="'.$telephone.'" , contact="'.$contact.'", item_date="'.$item_date.'" ');
				}
				else { 
					$db2->query('INSERT INTO posts SET api_id="'.$db_api_id.'", user_id="'.$db_user_id.'", group_id="'.$db_group_id.'", message="'.$db_message.'", mentioned="'.$db_mentioned.'", posttags="'.$db_posttags.'", attached="'.$db_attached.'", date="'.$db_date.'", date_lastcomment="'.$db_date.'", ip_addr="'.$db_ip_addr.'", city_id="'.$city_id.'", item_name="'.$item_name.'",item_link="'.$item_link.'", address="'.$address.'", telephone="'.$telephone.'" , contact="'.$contact.'" , item_date="'.$item_date.'"');
				}
				if( ! $id = $db2->insert_id() ) {
					return FALSE;
				}
				$this->attachments_copy_files();
				foreach($this->attached as $k=>$v) {
					$db2->query('INSERT INTO '.($is_private?'posts_pr_attachments':'posts_attachments').' SET post_id="'.$id.'", type="'.$db2->escape($k).'", data="'.$db2->escape(serialize($v)).'" ');
				}
				foreach($this->mentioned as $uid) {
					$db2->query('INSERT INTO '.($is_private?'posts_pr_mentioned':'posts_mentioned').' SET post_id="'.$id.'", user_id="'.intval($uid).'" ');
				}
				if( ! $is_private ) {
					$q	= array();
					$q2	= array();
					if( $this->user->id > 0 ) {
						$q[]	= '("'.$this->user->id.'", "'.$id.'")';
					}
					if($this->user->id > 0) {
						$u	= $this->network->get_user_follows($this->user->id)->followers;
						foreach($u as $k=>$v) {
							if( !$this->group || $this->group->is_public ) {
								switch($this->api_id)
								{
									case 2: $fancy_tab = 'feeds';
										break;
									case 6: $fancy_tab = 'tweets';
										break;
									default: $fancy_tab = 'all';
										break;
								}
								$q[]	= '("'.$k.'", "'.$id.'")';
								$q2[]	= array($k, $fancy_tab);
							}
						}
					}
					if( $this->group ) {
						$u	= $this->network->get_group_members($this->group->id);
						if($u) {
							foreach($u as $k=>$v) {
								switch($this->api_id)
								{
									case 2: $fancy_tab = 'feeds';
										break;
									case 6: $fancy_tab = 'tweets';
										break;
									default: $fancy_tab = 'all';
										break;
								}
								$q[]	= '("'.$k.'", "'.$id.'")';
								if( $k != $this->user->id ) {
									$q2[]	= array($k, $fancy_tab);
								}
							}
						}
						$q	= array_unique($q);					}
					if( count($q) > 0 ) {
						$q	= implode(', ', $q);
						
						switch($this->api_id)
						{
							case 2: $fancy_table = 'post_userbox_feeds';
								break;
							case 6: $fancy_table = 'post_userbox_tweets';
								break;
							default: $fancy_table = 'post_userbox';
						}
						$db2->query('INSERT INTO '.$fancy_table.' (user_id, post_id) VALUES '.$q);
					}
					if( count($q2) > 0 ) {
						$tmpu	= array();
						foreach($q2 as $tmptmp) {
							$tmpu[$tmptmp[0]][$tmptmp[1]]	= 1;
						}
						foreach($tmpu as $tmpuid=>$tmptabs) {
							foreach($tmptabs as $tmptab=>$tmpnum) {
								$this->network->set_dashboard_tabstate($tmpuid, $tmptab, $tmpnum);
							}
						}
					}
					if( $this->user->id > 0 ) {
						$db2->query('UPDATE users SET num_posts=num_posts+1, lastpost_date="'.time().'" WHERE id="'.$db_user_id.'" LIMIT 1');
					}
					if( $this->user->id > 0 && count($this->mentioned) > 0 ) {
						$notify	= array();
						if( !$this->group || $this->group->is_public ) {
							foreach($this->mentioned as $uid) {
								if( $uid == $this->user->id ) {
									continue;
								}
								$notify[]	= $uid;
							}
						}
						else {
							$grpmem	= $this->network->get_group_members($this->group->id);
							foreach($this->mentioned as $uid) {
								if( ! isset($grpmem[$uid]) ) {
									continue;
								}
								if( $uid == $this->user->id ) {
									continue;
								}
								$notify[]	= $uid;
							}
						}
						$notify	= array_unique($notify);
						global $page;
						$permalink	= $C->SITE_URL.'view/post:'.$id;
						$page->load_langfile('email/notifications.php');
						foreach($notify as $uid) {
							$this->network->set_dashboard_tabstate($uid, '@me', 1);				
							$n	= intval( $this->network->get_user_notif_rules($uid)->ntf_me_if_u_posts_qme );
							if( $n==1 || $n==3 ) {
								$ulng	= trim($this->network->get_user_by_id($uid)->language);
								$lng_txt	= array('#SITE_TITLE#'=>$C->SITE_TITLE, '#USER#'=>'@'.$this->user->username, '#NAME#'=>$this->user->fullname, '#A0#'=>$permalink, '#A1#'=>'', '#A2#'=>'');
								$lng_htm	= array('#SITE_TITLE#'=>$C->SITE_TITLE, '#USER#'=>'<a href="'.$C->SITE_URL.$this->user->username.'" title="'.htmlspecialchars($this->user->fullname).'" target="_blank">@'.$this->user->username.'</a>', '#NAME#'=>$this->user->fullname, '#A1#'=>'<a href="'.$permalink.'" target="_blank">', '#A2#'=>'</a>', '#A0#'=>'');
								$subject		= $page->lang('emlsubj_ntf_me_if_u_posts_qme', $lng_txt, $ulng);
								$message_txt	= $page->lang('emltxt_ntf_me_if_u_posts_qme', $lng_txt, $ulng);
								$message_htm	= $page->lang('emlhtml_ntf_me_if_u_posts_qme', $lng_htm, $ulng);
								$this->network->send_notification_email($uid, 'u_posts_qme', $subject, $message_txt, $message_htm);
							}
						}
					}
					if( $this->group ) {
						$db2->query('UPDATE groups SET num_posts=num_posts+1 WHERE id="'.$db_group_id.'" LIMIT 1');
					}
					if( $this->user->id > 0 ) {
						$db2->query('INSERT INTO '.($is_private?'posts_pr_comments_watch':'posts_comments_watch').' SET user_id="'.$this->user->id.'", post_id="'.$id.'", newcomments=0');
					}
					if( $C->RPC_PINGS_ON && $this->user->id>0 && $this->api_id!=2 && $this->api_id!=6 && (!$this->group || $this->group->is_public) ) {
						include_once($C->INCPATH.'classes/IXR_Library.inc.php');
						$myBlogName	= $this->user->username.' - '.$C->SITE_TITLE;
						$myBlogUrl	= $C->SITE_URL.$this->user->username;
						$myBlogUpdateUrl	= $C->SITE_URL.$this->user->username;
						$myBlogRSSFeedUrl	= $C->SITE_URL.'rss/username:'.$this->user->username;
						foreach($C->RPC_PINGS_SERVERS as $server) {
							$client	= new IXR_Client($server);
							$client->timeout	= 0;
							$client->useragent	.= ' -- '.$C->SITE_TITLE;
							$client->debug	= false;
							$client->query( 'weblogUpdates.extendedPing', $myBlogName, $myBlogUrl, $myBlogUpdateUrl, $myBlogRSSFeedUrl );
						}
					}
				}
				else {
					$db2->query('UPDATE users SET lastpost_date="'.time().'" WHERE id="'.$db_user_id.'" LIMIT 1');
					$this->network->set_dashboard_tabstate($db_to_user, 'private', 1);
					
					$n	= intval( $this->network->get_user_notif_rules($this->to_user->id)->ntf_me_if_u_posts_prvmsg );
					if( $n==1 || $n==3 ) {
						global $C, $page;
						$page->load_langfile('email/notifications.php');
						$permalink	= $C->SITE_URL.'view/priv:'.$id;
						$ulng	= trim($this->network->get_user_by_id($this->to_user->id)->language);
						$lng_txt	= array('#SITE_TITLE#'=>$C->SITE_TITLE, '#USER#'=>'@'.$this->user->username, '#NAME#'=>$this->user->fullname, '#A0#'=>$permalink, '#A1#'=>'', '#A2#'=>'');
						$lng_htm	= array('#SITE_TITLE#'=>$C->SITE_TITLE, '#USER#'=>'<a href="'.$C->SITE_URL.$this->user->username.'" title="'.htmlspecialchars($this->user->fullname).'" target="_blank">@'.$this->user->username.'</a>', '#NAME#'=>$this->user->fullname, '#A1#'=>'<a href="'.$permalink.'" target="_blank">', '#A2#'=>'</a>', '#A0#'=>'');
						$subject		= $page->lang('emlsubj_ntf_me_if_u_posts_prvmsg', $lng_txt, $ulng);
						$message_txt	= $page->lang('emltxt_ntf_me_if_u_posts_prvmsg', $lng_txt, $ulng);
						$message_htm	= $page->lang('emlhtml_ntf_me_if_u_posts_prvmsg', $lng_htm, $ulng);
						$this->network->send_notification_email($this->to_user->id, 'u_posts_prvmsg', $subject, $message_txt, $message_htm);
					}
					$db2->query('INSERT INTO '.($is_private?'posts_pr_comments_watch':'posts_comments_watch').' SET user_id="'.$this->user->id.'", post_id="'.$id.'", newcomments=0');
					$db2->query('INSERT INTO '.($is_private?'posts_pr_comments_watch':'posts_comments_watch').' SET user_id="'.$this->to_user->id.'", post_id="'.$id.'", newcomments=0');
				}
				$db2->query('UPDATE applications SET total_posts=total_posts+1 WHERE id="'.$db_api_id.'" LIMIT 1');
			}
			else
			{
				$id	= $this->id;
				$db2->query('UPDATE '.($is_private?'posts_pr':'posts').' SET message="'.$db_message.'", city_id="'.$city_id.'", item_name="'.$item_name.'", address="'.$address.'", item_date="'.$item_date.'",item_link="'.item_link.'", telephone="'.$telephone.'" , contact="'.$contact.'", mentioned="'.$db_mentioned.'", attached="'.$db_attached.'", posttags="'.$db_posttags.'", date_lastedit="'.time().'" WHERE id="'.$this->id.'" LIMIT 1');
				$db2->query('DELETE FROM '.($is_private?'posts_pr_attachments':'posts_attachments').' WHERE post_id="'.$this->id.'" ');
				$this->attachments_copy_files();
				foreach($this->attached as $k=>$v) {
					$db2->query('INSERT INTO '.($is_private?'posts_pr_attachments':'posts_attachments').' SET post_id="'.$id.'", type="'.$db2->escape($k).'", data="'.$db2->escape(serialize($v)).'" ');
				}
				$mentioned1	= array();
				$db2->query('SELECT user_id FROM '.($is_private?'posts_pr_mentioned':'posts_mentioned').' WHERE post_id="'.$this->id.'" ');
				while($sdf = $db2->fetch_object()) {
					$mentioned1[]	= intval($sdf->user_id);
				}
				$db2->query('DELETE FROM '.($is_private?'posts_pr_mentioned':'posts_mentioned').' WHERE post_id="'.$this->id.'" ');
				$mentioned2	= array();
				foreach($this->mentioned as $uid) {
					$db2->query('INSERT INTO '.($is_private?'posts_pr_mentioned':'posts_mentioned').' SET post_id="'.$id.'", user_id="'.intval($uid).'" ');
					$mentioned2[]	= intval($uid);
				}
				$new_mentioned	= array_diff($mentioned2, $mentioned1);
				foreach($new_mentioned as $uid) {
					if( $is_private ) {
						continue;
					}
					$this->network->set_dashboard_tabstate($uid, '@me', 1);
					// ...
				}
			}
			
			return $id ? ($id.($is_private?'_private':'_public')) : FALSE;
		}
 
	}
	
?>